﻿using System.Collections.Generic;

namespace eProject.ViewModel.Commands
{
    public class GroupCommand : ICommand
    {
        private readonly IEnumerable<ICommand> _commands;
        private readonly IEnumerable<ICommand> _rcommands;

        private bool _isCanceled;
        private bool _isExecuted;



        public GroupCommand(IEnumerable<ICommand> commands)
        {            
            _commands = new List<ICommand>(commands);
            var list = new List<ICommand>(commands);
            list.Reverse();
            _rcommands = list;
        }

        public void Execute()
        {
            if (_isExecuted) return;
            foreach (var command in _commands)
            {
                command.Execute();
            }
            _isExecuted = true;
            _isCanceled = false;
        }

        public void Unexecute()
        {
            if (_isCanceled) return;
            foreach (var command in _rcommands)
            {
                command.Unexecute();
            }
            _isCanceled = true;
            _isExecuted = false;
        }
    }
}


